package com.ymatou.iapi.m.couponservice.testcase;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.parameter.SendCouponBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymatou.iapi.m.couponservice.service.SendCouponCall;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponGenerateSubTask;
import com.ymttest.database.model.YmtCouponGenerateTask;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateSubTaskWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateTaskWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
/**
*商家任务发放优惠券用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendCoupon{
	private  SendCouponBean sendcouponBean;
	private  SendCouponCall sendcouponCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家任务发放优惠券用例");
	}

	@Before
	public void caseUp() {
		sendcouponBean=new SendCouponBean();
		sendcouponCall=new SendCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new SendCouponCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_001() {
		Logger.start(true,"商家任务发放优惠券-单用户");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();			
		
			VerifyReturn(sendcouponBean,sendcouponCall);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_002() {
		Logger.start(true,"商家任务发放优惠券-多用户");
		try {
			
			SendCouponBean sendcouponBean = new SendCouponBean();			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			
			SellerCreateBatchBean createCouponBean = couponCallService.setDefaultSellerCreateBatchBean();
		
			SellerCreateBatchCall  createCouponCall = couponCallService.SellerCreateBatch(createCouponBean);

			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean(createCouponBean.getSellerId(),createCouponCall.getBatchCode());

			HashSet<Integer> sendUsers = new HashSet<>();
			sendUsers.add(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			sendUsers.add(Integer.valueOf(EnvSetup.getData("couponBuyerId")));

			sendcouponBean.getTaskSendReqInfo().setSendUsers(sendUsers);
			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();			
		
			VerifyReturn(sendcouponBean,sendcouponCall);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_003() {
		Logger.start(true,"反例-批次编号不能为空");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setBatchCode(null);

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("2", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.batchCode:批次编号不能为空", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_004() {
		Logger.start(true,"反例-请传入要发放给哪些用户-null");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setSendUsers(null);

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("2", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.sendUsers:请传入要发放给哪些用户", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_005() {
		Logger.start(true,"反例-请传入要发放给哪些用户-空list");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setSendUsers(new HashSet<>());

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("2", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.sendUsers:发放用户数量须在1-100000之内", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_006() {
		Logger.start(true,"反例-请提供商家ID");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setSellerId(null);

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("2", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("请提供商家ID", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_007() {
		Logger.start(true,"反例-请提供用于显示在消息里面的商家名称");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setSellerShopName(null);

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("2", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.sellerShopName:请提供用于显示在消息里面的商家名称", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_008() {
		Logger.start(true,"反例-批次不存在");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setBatchCode("99999999");

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("200106", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次不存在", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_009() {
		Logger.start(true,"反例-商家创建发放任务，但批次不属于商家创建");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			sendcouponBean.getTaskSendReqInfo().setSellerId(1111);

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
//			Logger.verifyEquals("200100", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("商家创建发放任务，但批次不属于商家创建", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_010() {
		Logger.start(true,"反例-创建发放任务，优惠券获取方式不属于领取方式");
		try {
			
			SendCouponBean sendcouponBean = new SendCouponBean();			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean(123,createCouponCall.getBatchCode());

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
			Logger.verifyEquals("200112", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("创建发放任务，优惠券获取方式不属于领取方式", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_011() {
		Logger.start(true,"反例-批次已作废");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();
			
			CouponServiceMCallService.InvalidSellerBatch(sendcouponBean.getTaskSendReqInfo().getSellerId(),sendcouponBean.getTaskSendReqInfo().getBatchCode());

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
			Logger.verifyEquals("200102", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次已作废", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_012() {
		Logger.start(true,"反例-批次已过期");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			 // 设置优惠券过期
            Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            int iCouponSettingId =
                    new YmtCouponBatchWapper().selectBysBatchCode(sendcouponBean.getTaskSendReqInfo().getBatchCode()).get(0).getiCouponSettingId();
            new YmtCouponSettingWapper().setiEffectiveType(iCouponSettingId, 0);
            new YmtCouponSettingWapper().setdValidEnd(iCouponSettingId, date);
            
			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
			Logger.verifyEquals("200103", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次已过期", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_013() {
		Logger.start(true,"反例-批次已领完");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean();

			  YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sendcouponBean.getTaskSendReqInfo().getBatchCode()).get(0);
			  int iCouponSettingId =
	                    new YmtCouponBatchWapper().selectBysBatchCode(sendcouponBean.getTaskSendReqInfo().getBatchCode()).get(0).getiCouponSettingId();
	            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId, batch.getiCouponTotalNum());
	            

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
			Logger.verifyEquals("200104", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次已领完", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCoupon_014() {
		Logger.start(true,"反例-批次剩余券码不足(商品优惠券发放不判断剩余券码不足)");
		try {

			SendCouponBean sendcouponBean = new SendCouponBean();			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			
			SellerCreateBatchBean createCouponBean = couponCallService.setDefaultSellerCreateBatchBean();
			createCouponBean.setCouponTotalCount(1);
			SellerCreateBatchCall  createCouponCall = couponCallService.SellerCreateBatch(createCouponBean);

			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			sendcouponBean = CouponServiceMCallService.CreateDefaultSendCouponBean(createCouponBean.getSellerId(),createCouponCall.getBatchCode());

			HashSet<Integer> sendUsers = new HashSet<>();
			sendUsers.add(111111);
			sendUsers.add(222222);
			sendUsers.add(333333);
			sendUsers.add(444444);

			sendcouponBean.getTaskSendReqInfo().setSendUsers(sendUsers);

			sendcouponCall.setData(sendcouponBean);
			sendcouponCall.callService();	
			
			Logger.verifyEquals("0", sendcouponCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", sendcouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 
	 * 
	 * @param sendcouponBean
	 * @param sendcouponCall
	 * @throws InterruptedException
	 */
	private void VerifyReturn(SendCouponBean sendcouponBean, SendCouponCall sendcouponCall)
			throws InterruptedException {
		
		Logger.verifyEquals("0", sendcouponCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", sendcouponCall.getMessage(), "验证返回Message");

		String batchCode = sendcouponBean.getTaskSendReqInfo().getBatchCode();

		YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

		List<YmtCouponGenerateTask> tasks = new YmtCouponGenerateTaskWapper()
				.selectByCouponBatchId(batch.getiBatchId());

		Logger.verifyEquals(1, tasks.size(), "验证数据库表Ymt_CouponGenerateTask生成一条ibatch=" + batch.getiBatchId() + "的记录");

		if (tasks.size() == 1) {
			YmtCouponGenerateTask task = tasks.get(0);
			Logger.verifyEquals(sendcouponBean.getTaskSendReqInfo().getSendUsers().size(),
					task.getTaskGenerateCount(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(sendcouponBean.getTaskSendReqInfo().getOperatorId(), task.getTaskCreaterId(),
					"验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(sendcouponBean.getTaskSendReqInfo().getOperatorName(), task.getTaskCreaterName(),
					"验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(sendcouponBean.getTaskSendReqInfo().getBusinessType(),
					task.getTaskBusinessType().intValue(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(sendcouponBean.getTaskSendReqInfo().getSellerShopName(),
					task.getTaskSellerShopName(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");

			List<YmtCouponGenerateSubTask> subTasks = new YmtCouponGenerateSubTaskWapper()
					.selectByTaskId(task.getTaskId());

			Logger.verifyEquals(true, tasks.size() >= 1,
					"验证数据库表Ymt_CouponGenerateSubTask生成一条TaskId=" + task.getTaskId() + "的记录");

			List<YmtCoupon> coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());

			int flag = 0;
			while (coupons.size() == 0 && flag < 400) {
				Thread.sleep(200);
				coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
				flag++;
			}

			Logger.verifyEquals(true, coupons.size() >= 1,
					"验证数据库表Ymt_Coupon生成一条iBatchId=" + batch.getiBatchId() + "的记录");

			YmtCouponSetting setting = new YmtCouponSettingWapper()
					.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(sendcouponBean.getTaskSendReqInfo().getSendUsers().size(),
					setting.getiReceiveCount(), "验证数据库表Ymt_CouponSetting中iReceiveCount");

			HashSet<Integer> users = sendcouponBean.getTaskSendReqInfo().getSendUsers();

			for (int i = 0; i < coupons.size(); i++) {
				YmtCoupon coupon = coupons.get(i);
		
				// iCouponSetting
				Logger.verifyEquals(batch.getiCouponSettingId(), coupon.getiCouponSetting(),
						"验证数据库表Ymt_Coupon表中iCouponSetting");

				// 优惠券类型 2：链接
				Logger.verifyEquals(2, coupon.getiCouponType(), "验证数据库表Ymt_Coupon表中iCouponType");
				// 1：普通优惠卷
				Logger.verifyEquals(1, coupon.getiUsage(), "验证数据库表Ymt_Coupon表中iUsage");

				List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper()
						.selectBysCouponId(coupon.getsCouponId());

				Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(),
						"验证数据库表ymt_CouponPrivateUserBound生成一条sCouponId=" + coupon.getsCouponId() + "的记录");

				if (ymtCouponPrivateUserBounds.size() == 1) {
					YmtCouponPrivateUserBound ymtCouponPrivateUserBound = ymtCouponPrivateUserBounds.get(0);
					
					users.remove(ymtCouponPrivateUserBound.getiUserId());
				}
			}

			Logger.verifyEquals(0, users.size(), "验证数据库表ymt_CouponPrivateUserBound生成了传人所有SendUsers的优惠券");

		}

	}
}